<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Carousel</title>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <div class="container">
      <div id="side">
        <div class="item" style="background-image: url('./images/1.png')"></div>
        <div class="item" style="background-image: url('./images/2.png')"></div>
        <div class="item" style="background-image: url('./images/3.png')"></div>
        <div class="item" style="background-image: url('./images/4.png')"></div>
        <div class="item" style="background-image: url('./images/5.png')"></div>
        <div class="item" style="background-image: url('./images/6.png')"></div>
      </div>
      <div class="buttons">
        <div class="left">< Prev</div>
        <div class="right">Next ></div>
      </div>
    </div>
  </body>
  <script>
    const leftBtn = document.querySelector('.buttons .left')
    const rightBtn = document.querySelector('.buttons .right')
    const slide = document.querySelector('#side')
    let openClick = true // 防止多次点击

    rightBtn.addEventListener('click', () => {
      if (openClick) {
        openClick = false
        const items = document.querySelectorAll('.item')
        slide.appendChild(items[0]) // 将第一个元素添加到slide中
        setTimeout(() => {
          openClick = true
        }, 1000)
      }
    })
    leftBtn.addEventListener('click', () => {
      if (openClick) {
        openClick = false
        const items = document.querySelectorAll('.item')
        slide.prepend(items[items.length - 1])
        setTimeout(() => {
          openClick = true
        }, 1000)
      }
    })
  </script>
</html>
